package com.edu.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.Drun;
import com.edu.entity.Trun;
import com.edu.service.DrunService;
import com.edu.service.TrunService;
import com.edu.service.Impl.DrunServiceImpl;
import com.edu.service.Impl.TrunServiceImpl;

/**
 * Servlet implementation class DrunServletAll
 */
@WebServlet("/DrunServletAll")
public class DrunServletAll extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DrunServletAll() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("1111111111");
		request.setCharacterEncoding("UTF-8");
		String menu = request.getParameter("menu");
		System.out.println(menu);
		//查询
		if ("1".equals(menu)) {
			queryByUser(request,response);
		//修改
		}else if("2".equals(menu)) {
			userUpdate(request, response);
		//添加
		}else if("3".equals(menu)) {
			userAdd(request, response);
		//删除
		}else if("4".equals(menu)) {
			userDelete(request, response);
		}else {
			System.out.println("sorry!");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	//查询
	protected void queryByUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int DrunId = -1;
		if(!("".equals(request.getParameter("DrunId")))){
			DrunId = Integer.parseInt(request.getParameter("DrunId"));
		}
		String dJob = request.getParameter("dJob");
		String dName = request.getParameter("dName");
		String dCost = request.getParameter("dCost");
		Drun drun = new Drun(DrunId, dJob, dName, dCost);
		DrunService drunServlet = new DrunServiceImpl();
		List<Drun> list = drunServlet.selectByDrun(drun);
		request.setAttribute("list",list);
		//response.sendRedirect("index.jsp");
		request.getRequestDispatcher("Drun.jsp").forward(request, response);
	}
		
	//修改
	protected void userUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int DrunId = Integer.parseInt(request.getParameter("DrunId"));
		String dJob = request.getParameter("dJob");
		String dName = request.getParameter("dName");
		String dCost = request.getParameter("dCost");
		Drun drun = new Drun(DrunId, dJob, dName, dCost);
		DrunService drunServlet = new DrunServiceImpl();
		drunServlet.updateDrun(drun);
		response.sendRedirect("Drun.jsp");
	}
		
	//添加
	protected void userAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String dJob = request.getParameter("dJob");
		String dName = request.getParameter("dName");
		String dCost = request.getParameter("dCost");
		Drun drun = new Drun(dJob, dName, dCost);
		DrunService drunServlet = new DrunServiceImpl();
		drunServlet.addDrun(drun);
		response.sendRedirect("Drun.jsp");
	}
		
	//删除
	protected void userDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String[] ids = request.getParameterValues("selectdid");

		DrunService drunServlet = new DrunServiceImpl();
		drunServlet.deleteDrunAll(ids);
		response.sendRedirect("Drun.jsp");
	}

}
